<template>
  <div>
    <el-form @submit.native.prevent ref="editForm" :model="formFileds">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item>
            <el-input v-model="input" prefix-icon="el-icon-search" placeholder="查询文档名称或标准类型"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" @click="get()"> 查询 </el-button>
            <el-button type="primary" @click="add()"> 新增 </el-button>
            <el-button type="primary" @click="deleteSelection">
              批量删除
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--表格内容-->
    <el-table ref="list" :data="tableData" style="width: 100%; margin-bottom: 15px" border highlight-current-row
      :default-sort="{ prop: 'date', order: 'descending' }" @select-all="handleSelectionChange"
      @select="handleSelectionChange" @selection-change="handleSelectionChange"
      :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
      <el-table-column fixed type="selection"> </el-table-column>
      <el-table-column property="id" label="ID"> </el-table-column>
      <el-table-column property="companyName" label="公司名称">
      </el-table-column>
      <el-table-column property="name" label="文档名称"> </el-table-column>
      <el-table-column property="url" label="文档地址"> </el-table-column>
      <el-table-column property="type" label="标准类型"> </el-table-column>
      <el-table-column property="price" label="文档价格(元)"> </el-table-column>
      <el-table-column property="createTime" label="发布时间">
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="rowEdit(scope.row)" size="mini">编辑
          </el-button>
          <el-button type="danger" size="mini" @click="rowDel(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination :page-sizes="[10, 20, 30, 40, 100]" :page-size="pageSize" :total="total"
      @current-change="currentChange" @size-change="sizeChange" layout="total, sizes, prev, pager, next, jumper"
      style="padding-bottom: 15px">
    </el-pagination>

    <FileManageAdd ref="fileManageAdd" v-if="isShowAddDialog" :isShowAddDialog="isShowAddDialog"
      @dialogClose="dialogClose" @close="get()"></FileManageAdd>
    <FileManageEdit ref="fileManageEdit" v-if="isShowEditDialog" :isShowEditDialog="isShowEditDialog" :record="formFileds"
      @dialogClose="dialogClose" @close="get()"></FileManageEdit>
  </div>
</template>

<script>
import fileManageAdd from "./fileManageAdd.vue";
import fileManageEdit from "./fileManageEdit.vue";
import {
  getTextFile,
  deleteDocument,
  deleteBatchDocument,
} from "@/api/systemManage/documentManage";
import FileManageAdd from "./fileManageAdd.vue";
import FileManageEdit from "./fileManageEdit.vue";
export default {
  name: "documentManage",
  components: {
    FileManageAdd,
    FileManageEdit,
  },
  data () {
    return {
      text: "",
      formFileds: {},
      tableData: [],
      isShowAddDialog: false,
      isShowEditDialog: false,
      multipleSelection: null,
      pageSize: 10,
      pageNum: 1,
      total: 0,
      input: "",
    };
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    handleCheckedAllAndCheckedNone (selection) {
      // 当前选中仅一行时操作-（当前表格行高亮）
      1 != selection.length && this.$refs.list.setCurrentRow();
    },
    dialogClose () {
      this.isShowAddDialog = false;
      this.isShowEditDialog = false;
    },
    rowEdit (row) {
      // //当前选中行
      this.formFileds = row;
      this.isShowEditDialog = true;
    },
    // 选中当前行-当前行的复选框被勾选
    setCurRowChecked (row) {
      this.$refs.list.clearSelection();
      this.$refs.list.toggleRowSelection(row);
    },
    add () {
      this.isShowAddDialog = true;
    },
    handleEdit (id) {
      this.$refs.editForm.validate((isValid) => {
        if (!isValid) return;
        // 保存编辑后的数据
        Object.assign(this.tableData[id], this.formFileds);
        this.isShowEditDialog = false;
        // 考虑到可能编辑了日期-需要重新排序
        // ***注意：手动排序传参和表格的default-sort属性格式不太一样
        this.$refs.list.sort("date", "descending");

        this.$message.success("编辑成功");
      });
    },
    // 删除
    rowDel (id) {
      // 让当前删除按钮失焦
      // event.target.blur();

      this.$confirm("您确定要删除该文档吗？", "删除", {
        comfirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        deleteDocument("id=" + id).then((res) => {
          if (res) {
            this.$message.success("删除成功！");
            this.get();
          }
        });
      });
    },
    // 批量删除
    deleteSelection () {
      if (!this.multipleSelection) {
        this.$message.error("请至少选择一条数据进行删除！");
        return;
      } else {
        this.$confirm("数据将会永久删除，是否继续？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }).then(() => {
          //生命空数组，这里的数组是后端需要的数据
          let array = [];
          //遍历勾选数据取自己需要的id等 这里是和后端需要的参数对应
          for (let i = 0; i < this.multipleSelection.length; i++) {
            let obj = this.multipleSelection[i].id;
            array.push(obj); //把取出来的对象push到数组里
          }
          let obj = { id: array };
          deleteBatchDocument(obj).then((res) => {
            //接口
            this.get(); //删除完成后初始化查询列表
          });
        });
      }
    },
    // 查询
    get () {
      var par;
      par = {
        input: this.input,
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      };
      getTextFile(par).then((res) => {
        if (res) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          for (let i = 0; i < this.tableData.length; i++) {
            let newdate = this.tableData[i].createTime;
            this.tableData[i].createTime = newdate.substring(0, 10);
          }
        }
      });
    },
    // 当前页
    currentChange (e) {
      this.pageNum = e;
      this.get();
    },
    // 页面显示行数
    sizeChange (e) {
      this.pageSize = e;
      this.get();
    },
  },
  mounted () {
    this.get();
  },
};
</script>

<style scoped lang="less">
.el-form {
  padding: 0 10px;
}

.el-date-editor {
  width: 100% !important;
}
</style>
